package com.xiaoyu.client.ui.fragment.main;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toolbar;

import com.xiaoyu.client.R;
import com.xiaoyu.commonlib.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeFragments extends BaseFragment {
    @BindView(R.id.recycler_image)
    RecyclerView mRecyclerImage;
    //图片地址

    private static final String SAVEAPKURL = "mnt/sdcard/zhsq" + System.currentTimeMillis() + ".apk";

    private String mTitle, sqname;
    View view;

    private CollapsingToolbarLayout collapsing_toolbar;
    private AppBarLayout appBarLayout;

    private Toolbar toolbar;
    private GridView mRecyclerView;

    private TextView sqwz;
    private CollapsingToolbarLayoutState state;
    private ViewPager viewPager;
    private ViewPager viewPager1;


    List<String> mStrings;//轮波图结合

    private enum CollapsingToolbarLayoutState {
        EXPANDED,
        COLLAPSED,
        INTERNEDIATE
    }

    public static HomeFragment getInstance(String title) {
        HomeFragment sf = new HomeFragment();
        return sf;
    }

    @Override
    public void onResume() {
        super.onResume();
//        if (sqname.equals("")) {
//            sqwz.setText("选择社区");
//        } else {
//            sqwz.setText(sqname);
//        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.layout_home, null);
        ButterKnife.bind(this, view);

        mStrings = new ArrayList<>();

        init();
//        sqwz.setText(sqname);

        return view;
    }


    private String updataCode = "updataCode";


    private void init() {

        //生活服务、家政服务viewpager画廊效果
        viewpager();

        //浮动按钮
        view.findViewById(R.id.btn_12349).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        //定位社区
        sqwz = view.findViewById(R.id.home_sqwz_tv);
        sqwz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent(getActivity(), LocationActivity.class);
//                intent.putExtra("sqwz", sqwz.getText().toString());
//                startActivity(intent);
            }
        });

        //标题栏相关
        appBar();


        mRecyclerView = (GridView) view.findViewById(R.id.rv_grid_view);

    }

    //标题栏相关
    private void appBar() {
        appBarLayout = view.findViewById(R.id.appbarlayout);
        collapsing_toolbar = view.findViewById(R.id.collapsing_toolbar);
        //Toolbar的设置
//        toolbar = view.findViewById(R.id.toolbar);

        appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

                if (verticalOffset == 0) {
                    if (state != CollapsingToolbarLayoutState.EXPANDED) {
                        state = CollapsingToolbarLayoutState.EXPANDED;//修改状态标记为展开
                        collapsing_toolbar.setTitle("EXPANDED");//设置title为EXPANDED
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (state != CollapsingToolbarLayoutState.COLLAPSED) {
                        collapsing_toolbar.setTitle("");//设置title不显示
                        sqwz.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_home_shequ_zhedie));
                        sqwz.setTextColor(getResources().getColor(R.color.color_gray_99));
                        sqwz.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.icon_dingwei_gray), null, getResources().getDrawable(R.mipmap.icon_function), null);

                        state = CollapsingToolbarLayoutState.COLLAPSED;//修改状态标记为折叠
                    }
                } else {
                    if (state != CollapsingToolbarLayoutState.INTERNEDIATE) {
                        if (state == CollapsingToolbarLayoutState.COLLAPSED) {
                            sqwz.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_home_shequ));
                            sqwz.setTextColor(getResources().getColor(R.color.color_white));
                            sqwz.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.mipmap.icon_dingwei_blue), null, getResources().getDrawable(R.mipmap.icon_examine_order_relation), null);

                        }
                        collapsing_toolbar.setTitle("INTERNEDIATE");//设置title为INTERNEDIATE
                        state = CollapsingToolbarLayoutState.INTERNEDIATE;//修改状态标记为中间
                    }
                }
            }
        });
    }

    private int pagerWidth;


    /**
     * 生活服务、家政服务
     */
    private void viewpager() {
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);

        viewPager1 = (ViewPager) view.findViewById(R.id.viewPager1);


        viewPager.setOffscreenPageLimit(3);
        viewPager1.setOffscreenPageLimit(3);


        pagerWidth = (int) (getResources().getDisplayMetrics().widthPixels * 3.0f / 5.0f);


        ViewGroup.LayoutParams lp = viewPager.getLayoutParams();

        ViewGroup.LayoutParams lp1 = viewPager1.getLayoutParams();

        if (lp == null) {
            lp = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            lp.width = pagerWidth;
        }

        if (lp1 == null) {
            lp1 = new ViewGroup.LayoutParams(pagerWidth, ViewGroup.LayoutParams.MATCH_PARENT);
        } else {
            lp1.width = pagerWidth;
        }

        viewPager.setLayoutParams(lp);
        viewPager.setPageMargin(-180);

        viewPager1.setLayoutParams(lp1);
        viewPager1.setPageMargin(-180);


        view.findViewById(R.id.shfw_ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return viewPager.dispatchTouchEvent(motionEvent);
            }
        });


        view.findViewById(R.id.jzfw_ll).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return viewPager1.dispatchTouchEvent(motionEvent);
            }
        });



        viewPager.setCurrentItem(1);
        viewPager1.setCurrentItem(1);

    }


    @Override
    public void onStop() {
        super.onStop();
    }

}